package com.yedean.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yedean.mall.dao.UmsAdminDao;
import com.yedean.mall.entity.UmsAdmin;
import com.yedean.mall.entity.UmsAdminRoleRelation;
import com.yedean.mall.entity.UmsRole;
import com.yedean.mall.mapper.UmsAdminMapper;
import com.yedean.mall.mapper.UmsAdminRoleRelationMapper;
import com.yedean.mall.service.IUmsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 后台用户表 服务实现类
 * </p>
 *
 * @author yedean
 * @since 2022-08-03
 */
@Service
public class UmsAdminServiceImpl extends ServiceImpl<UmsAdminMapper, UmsAdmin> implements IUmsAdminService {
    
    @Resource
    private UmsAdminRoleRelationMapper adminRoleRelationMapper;
    @Resource
    private UmsAdminDao umsAdminDao;

    @Override
    public void updateRole(Long adminId, List<Long> roleIds) {

        QueryWrapper<UmsAdminRoleRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("admin_id",adminId);

        adminRoleRelationMapper.delete(wrapper);

        //建立新关系
        roleIds.forEach(roleId ->{
            UmsAdminRoleRelation relation = new UmsAdminRoleRelation();
            relation.setAdminId(adminId);
            relation.setRoleId(roleId);
            adminRoleRelationMapper.insert(relation);
        });
    }

    @Override
    public List<UmsRole> getRoleList(Long adminId) {
       return umsAdminDao.getRoleList(adminId);
    }
}
